#!/bin/bash

# set -e

# 参数、常量定义区 ###################################################################################################################################
LOG_LEVEL="INFO"
# INLET_HOST=192.168.152.101
APP_HOME=$(pwd)
DB_PATH=$(pwd)/sqlite.db
LOG_PATH=$(pwd)/logs/icustom.log
APP_HOME_DIR=""
APP_PATH=$(pwd)/bigdata-install/bigdata-install-server/target
APP_NAME=bigdata-install.jar
APP_PORT="18086"

VM_OPTS="-Xms515m -Xmx1024m -Dfile.encoding=UTF-8 -Duser.timezone=GMT+08 -Dserver.port=$APP_PORT"
# PROGRAM_ARGUMENTS="--spring.profiles.active=prod --server.port=$APP_PORT --LOG_LEVEL=$LOG_LEVEL --INLET_HOST=$INLET_HOST --APP_HOME=$APP_HOME --DB_PATH=$DB_PATH"
PROGRAM_ARGUMENTS="--spring.profiles.active=prod --server.port=$APP_PORT --LOG_LEVEL=$LOG_LEVEL --APP_HOME=$APP_HOME --DB_PATH=$DB_PATH"
psid=0

# 日期字符串形式
date_str=$date_str1 $date_str2
date_str1=$(date +%Y-%m-%d)
date_str2=$(date +%H:%M:%S)
echo $date_str 

JDK_TAR=app/jdk-8u371-linux-x64.tar.gz
JDK_FO=jdk1.8.0_371
# 参数、常量定义区 ###################################################################################################################################

## 如果没有安装jdk,应该返回：-bash: java: command not found
## 如果已经安装jdk,应该返回：java version "1.8.0_371"
##                           Java(TM) SE Runtime Environment (build 1.8.0_371-b11)
function installJdk(){ 

echo "**********************install jdk*************************"
    echo "install JDK ......"
    
    tar -zxvf $JDK_TAR
    rm -rf /usr/java && mkdir -p /usr/java
    mv $JDK_FO /usr/java/jdk1.8.0

    ln -s /usr/java/jdk1.8.0/ /usr/java/latest
    ln -s /usr/java/latest /usr/java/default
    ln -s /usr/java/latest/bin/java /usr/bin/
    ln -s /usr/java/latest/bin/jps /usr/bin/

    echo 'export JAVA_HOME=/usr/java/jdk1.8.0' >> /etc/profile
    echo 'export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar' >> /etc/profile
    echo 'export PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
    echo 'JAVA_HOME=/usr/java/latest/' >> /etc/environment

    source /etc/profile
    rm -rf $JDK_FO 

    systemctl daemon-reload
    sysctl -p
    echo "*****************************************************"
    java -version
    echo "*****************************************************"
    echo "jdk is install successfull..."
    echo "*****************************************************"
}

fileClean() {
   rm -rf logs
   systemctl daemon-reload
}

checkpid() {
   javaps=$(pgrep -f "${APP_NAME}")

   if [ -n "$javaps" ]; then
      psid=$javaps
   else
      psid=0
   fi
}

# echo "$VM_OPTS -jar $APP_PATH/$APP_NAME $PROGRAM_ARGUMENTS"
start() {
   checkpid

   if [ $psid -ne 0 ]; then
      echo "===================================================="
      echo "warn: $APP_NAME already started! (pid=$psid)"
      echo "===================================================="
   else
      echo "Starting $APP_NAME ..."
      nohup java $VM_OPTS -jar $APP_PATH/$APP_NAME $PROGRAM_ARGUMENTS >/dev/null 2>&1 &
      sleep 1
      checkpid
      if [ $psid -ne 0 ]; then
         echo "(pid=$psid) [OK]"
      else
         echo "[Failed]"
      fi
   fi
}

stop() {
   checkpid

   if [ $psid -ne 0 ]; then
      echo -n "Stopping $APP_NAME ...(pid=$psid) "
      kill -9 $psid

      if [ $? -eq 0 ]; then
         echo "[OK]"
      else
         echo "[Failed]"
      fi

      checkpid
      if [ $psid -ne 0 ]; then
         stop
      fi
   else
      echo "===================================================="
      echo "warn: $APP_NAME is not running"
      echo "===================================================="
   fi
}

status() {
   checkpid
   if [ $psid -ne 0 ];  then
      echo "$APP_NAME is running! (pid=$psid)"
   else
      echo "$APP_NAME is not running"
   fi
}

# 输出帮助信息
function help(){
    echo ""
    echo "./start.sh start    启动java服务"
    echo "./start.sh stop     停止java服务"
    echo "./start.sh restart  重启java服务"
    echo "./start.sh status   查看java服务状态"
    echo "./start.sh log      启动查看运行日志"
    echo "./start.sh clean    清理运行日志"
    echo ""
}

showlog() {
   tail -f -n 30 ${LOG_PATH}
}


echo "当前启动服务器 os: "
cat /etc/system-release

if ! type java >/dev/null 2>&1; then
  if [ ! -f /usr/java/jdk1.8.0 ] ;then
      installJdk
  else
      echo 'JDK 已安装 环境参数未生效 需要执行: source /etc/profile';
  fi
#else
#  echo 'JDK 已安装';
fi

# 如果数据库文件不存在,建立数据库文件
if [ ! -f $DB_PATH ] ;then
  touch $DB_PATH
fi

case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;
   'restart')
     stop
     start
     ;;
   'status')
     status
     ;;
   'help')
     help
     ;;
   'clean')
     fileClean
     ;;
   'log')
     showlog
     ;;
  *)
     echo "Usage: $0 {start | stop | restart | status | help | log | clean}"
     exit 1
esac

exit 0

